<html>
<head><meta charset="utf-8"><title>Assign to slice · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Assign.20to.20slice.html">Assign to slice</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="208565663"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Assign%20to%20slice/near/208565663" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gsurrel <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Assign.20to.20slice.html#208565663">(Aug 31 2020 at 14:40)</a>:</h4>
<p>Hi!<br>
I've been programming in Rust for some months now and I've encountered some unexpected difficulty, which is assigning to a slice. Let's take that example, which fails to compile because there is no ".set(...)":</p>
<div class="codehilite"><pre><span></span><code><span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="n">the_string</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s">&quot;This is my string&quot;</span><span class="p">.</span><span class="n">to_string</span><span class="p">();</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">replacement_str</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s">&quot;your&quot;</span><span class="p">;</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">slice</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">&amp;</span><span class="k">mut</span><span class="w"> </span><span class="n">the_string</span><span class="p">[</span><span class="mi">8</span><span class="o">..</span><span class="mi">10</span><span class="p">];</span><span class="w"></span>
<span class="w">    </span><span class="n">slice</span><span class="p">.</span><span class="n">set</span><span class="p">(</span><span class="n">replacement_str</span><span class="p">);</span><span class="w"></span>
<span class="w">    </span><span class="n">println</span><span class="o">!</span><span class="p">(</span><span class="s">&quot;{}&quot;</span><span class="p">,</span><span class="w"> </span><span class="o">&amp;</span><span class="n">the_string</span><span class="p">);</span><span class="w"> </span><span class="c1">// I want &quot;This is your string&quot;</span>
</code></pre></div>


<p>How would I idiomatically do that?</p>



<a name="208565712"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Assign%20to%20slice/near/208565712" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Assign.20to.20slice.html#208565712">(Aug 31 2020 at 14:41)</a>:</h4>
<p><a href="https://doc.rust-lang.org/std/primitive.slice.html#method.copy_from_slice">https://doc.rust-lang.org/std/primitive.slice.html#method.copy_from_slice</a></p>



<a name="208565887"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Assign%20to%20slice/near/208565887" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gsurrel <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Assign.20to.20slice.html#208565887">(Aug 31 2020 at 14:42)</a>:</h4>
<p>I know about the slice.replace("my", "your"), but I can't use that in my case.</p>



<a name="208565903"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Assign%20to%20slice/near/208565903" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gsurrel <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Assign.20to.20slice.html#208565903">(Aug 31 2020 at 14:42)</a>:</h4>
<p><span class="user-mention" data-user-id="232545">@Joshua Nelson</span> Thanks, I'll check that out!</p>



<a name="208566333"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Assign%20to%20slice/near/208566333" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gsurrel <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Assign.20to.20slice.html#208566333">(Aug 31 2020 at 14:45)</a>:</h4>
<p>The <code>copy_from_slice</code> doesn't work in that case, as the lengths must match: "The length of src must be the same as self.".</p>



<a name="208566448"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Assign%20to%20slice/near/208566448" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Assign.20to.20slice.html#208566448">(Aug 31 2020 at 14:46)</a>:</h4>
<p>what do you expect it to do if the lengths don't match?</p>



<a name="208566663"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Assign%20to%20slice/near/208566663" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Assign.20to.20slice.html#208566663">(Aug 31 2020 at 14:47)</a>:</h4>
<p>oh I see you want it to replace</p>



<a name="208566725"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Assign%20to%20slice/near/208566725" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gsurrel <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Assign.20to.20slice.html#208566725">(Aug 31 2020 at 14:48)</a>:</h4>
<p>I would expect the same behavior as str.replace(), except I do not provide a string to replace, but the range to replace.</p>



<a name="208566740"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Assign%20to%20slice/near/208566740" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Assign.20to.20slice.html#208566740">(Aug 31 2020 at 14:48)</a>:</h4>
<p><code>the_string.replace("my", "your")</code> maybe</p>



<a name="208566835"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Assign%20to%20slice/near/208566835" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Assign.20to.20slice.html#208566835">(Aug 31 2020 at 14:48)</a>:</h4>
<p>hmm I don't know if there's something in std that takes an index range</p>



<a name="208566910"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Assign%20to%20slice/near/208566910" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Assign.20to.20slice.html#208566910">(Aug 31 2020 at 14:48)</a>:</h4>
<p>oh look at that <span aria-label="laughing" class="emoji emoji-1f606" role="img" title="laughing">:laughing:</span> <a href="https://doc.rust-lang.org/std/string/struct.String.html#method.replace_range">https://doc.rust-lang.org/std/string/struct.String.html#method.replace_range</a></p>



<a name="208567037"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Assign%20to%20slice/near/208567037" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Assign.20to.20slice.html#208567037">(Aug 31 2020 at 14:49)</a>:</h4>
<p>only on String, not &amp;str, but maybe that's enough for your use case</p>



<a name="208567269"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Assign%20to%20slice/near/208567269" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gsurrel <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Assign.20to.20slice.html#208567269">(Aug 31 2020 at 14:51)</a>:</h4>
<p>Hummm, I'm deep into slices not to have to track the indices manually, but if that's what I need, I'll switch to that :|<br>
Thanks for the links, though!</p>



<a name="208568173"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Assign%20to%20slice/near/208568173" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Assign.20to.20slice.html#208568173">(Aug 31 2020 at 14:56)</a>:</h4>
<p>you can't do this with slices because the strings aren't the same size</p>



<a name="208568193"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Assign%20to%20slice/near/208568193" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Assign.20to.20slice.html#208568193">(Aug 31 2020 at 14:56)</a>:</h4>
<p>it will require reallocation one way or another</p>



<a name="208568233"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Assign%20to%20slice/near/208568233" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Assign.20to.20slice.html#208568233">(Aug 31 2020 at 14:56)</a>:</h4>
<p>although I'm a little surprised it's only implemented on String, <code>str::replace</code> is a thing</p>



<a name="208568510"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Assign%20to%20slice/near/208568510" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Assign.20to.20slice.html#208568510">(Aug 31 2020 at 14:58)</a>:</h4>
<p>might be a good first PR ;)</p>



<a name="208568606"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Assign%20to%20slice/near/208568606" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gsurrel <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Assign.20to.20slice.html#208568606">(Aug 31 2020 at 14:59)</a>:</h4>
<p>Yep, I found it surprising too. Seeing replace(), I was expecting something "less" advanced, providing the range to replace</p>



<a name="208568805"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Assign%20to%20slice/near/208568805" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gsurrel <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Assign.20to.20slice.html#208568805">(Aug 31 2020 at 15:00)</a>:</h4>
<p>Actually, replace() returns a new String, so it's not replacing in the original one! That's how it's done (which doesn't help me here)</p>



<a name="208569029"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Assign%20to%20slice/near/208569029" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gsurrel <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Assign.20to.20slice.html#208569029">(Aug 31 2020 at 15:01)</a>:</h4>
<p>I think I'll have to first loop to find all my delimiters for replacements ranges, and then mutate my string. If I loop and mutate, I've a lot of borrowing and mutalibity issues.</p>



<a name="208569308"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Assign%20to%20slice/near/208569308" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Assign.20to.20slice.html#208569308">(Aug 31 2020 at 15:03)</a>:</h4>
<p><span class="user-mention silent" data-user-id="269970">gsurrel</span> <a href="#narrow/stream/122651-general/topic/Assign.20to.20slice/near/208568805">said</a>:</p>
<blockquote>
<p>Actually, replace() returns a new String, so it's not replacing in the original one! That's how it's done (which doesn't help me here)</p>
</blockquote>
<p>correct, that's what I mean there's no way to do this with slices</p>



<a name="208569333"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Assign%20to%20slice/near/208569333" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Assign.20to.20slice.html#208569333">(Aug 31 2020 at 15:04)</a>:</h4>
<p>because the new string could be bigger or smaller than the original</p>



<a name="208577249"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Assign%20to%20slice/near/208577249" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Assign.20to.20slice.html#208577249">(Aug 31 2020 at 15:59)</a>:</h4>
<p><span class="user-mention" data-user-id="269970">@gsurrel</span> it's an important point — do you understand how your request is impossible?</p>



<a name="208577602"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Assign%20to%20slice/near/208577602" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Assign.20to.20slice.html#208577602">(Aug 31 2020 at 16:01)</a>:</h4>
<p>If you have a cardboard box with 4 slots in it, with items A, B, C, D in the slots, you can't "replace BC with XYZ" because there's literally nowhere to put those things in that container.</p>



<a name="208577771"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Assign%20to%20slice/near/208577771" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Assign.20to.20slice.html#208577771">(Aug 31 2020 at 16:02)</a>:</h4>
<p>You <em>have</em> to get a new cardboard box with more slots and move / copy the items from the first box.</p>



<a name="208577949"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Assign%20to%20slice/near/208577949" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Assign.20to.20slice.html#208577949">(Aug 31 2020 at 16:03)</a>:</h4>
<p><a href="https://en.wikipedia.org/wiki/Pigeonhole_principle">https://en.wikipedia.org/wiki/Pigeonhole_principle</a></p>



<a name="208583330"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Assign%20to%20slice/near/208583330" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gsurrel <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Assign.20to.20slice.html#208583330">(Aug 31 2020 at 16:35)</a>:</h4>
<p><span class="user-mention" data-user-id="116155">@Jake Goulding</span> Absolutely, I had it in mind since the beginning, but I got confused by the existence of the <code>str.replace(a, b)</code> as I did not see immediately that it was returning a new string. <br>
In the end, I've implemented what I described afterwards: I first scan my string for all the replacements positions and values that I store in a vec, and then mutate the original string as all the slices are now out of scope (thanks to NLL). More memory usage, but it's not a problem in my case.</p>



<a name="208583757"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Assign%20to%20slice/near/208583757" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Assign.20to.20slice.html#208583757">(Aug 31 2020 at 16:38)</a>:</h4>
<p>It also sounds like you have multiple replacements, so you likely needed such a two-pass algorithm anyway. For example, if the initial string was <code>one</code> and you had <code>{one =&gt; two, two =&gt; three}</code>, what would the desired output be?</p>



<a name="208584522"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Assign%20to%20slice/near/208584522" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gsurrel <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Assign.20to.20slice.html#208584522">(Aug 31 2020 at 16:43)</a>:</h4>
<p>I'm actually having a string that has several replacements to do, but independent ones:<br>
It's something similar to "_A_something I don't know_A__B_another thing_B__C_something else_C_"<br>
That needs to be replaced as follows: "_A_new value_A__B_other value_B__C_last value_C"</p>



<a name="208584906"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Assign%20to%20slice/near/208584906" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Assign.20to.20slice.html#208584906">(Aug 31 2020 at 16:46)</a>:</h4>
<p>Sure, but that doesn't seem to address my point, unless you are claiming that the strings to search for and replace with are completely under your control. Using your example, if the user says that <code>something I don't know</code> should be replaced with <code>something else</code> <em>and</em> <code>something else</code> should also have some replacement, then doing it iteratively has a different result than two-pass.</p>



<a name="208585646"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Assign%20to%20slice/near/208585646" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gsurrel <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Assign.20to.20slice.html#208585646">(Aug 31 2020 at 16:52)</a>:</h4>
<p>It's not about replacing "something I don't know", it's about replacing whatever is between the _A_ delimiters (and they cannot be injected as they are escaped). Same goes for B and C. In practice, it's not delimited like that, but it's easier to explain like that. Sorry for being unclear, it's sometimes difficult to communicate properly <span aria-label="sweat smile" class="emoji emoji-1f605" role="img" title="sweat smile">:sweat_smile:</span></p>



<a name="208585685"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Assign%20to%20slice/near/208585685" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Assign.20to.20slice.html#208585685">(Aug 31 2020 at 16:53)</a>:</h4>
<p>Gotcha.</p>



<a name="208585864"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Assign%20to%20slice/near/208585864" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Assign.20to.20slice.html#208585864">(Aug 31 2020 at 16:54)</a>:</h4>
<p>Depending on your overall goals, you can also have a <code>Vec&lt;&amp;str&gt;</code> or a <code>Vec&lt;Cow&lt;str&gt;&gt;</code> or a <code>String</code></p>



<a name="208586130"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Assign%20to%20slice/near/208586130" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gsurrel <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Assign.20to.20slice.html#208586130">(Aug 31 2020 at 16:56)</a>:</h4>
<p>Actually, the full story is the following: I've a LibreOffice file saved as Flat ODT that serves as a template, where I need to update some fields given some inputs from command-line arguments. I started by using xml_rs and xmltree to load and parse the data (successfully), but I completely failed at mutating the tree and saving it. I then tried with sxd-document to have DOM-like manipulation, but just loading and saving my template document corrupted it, so I didn't continue.</p>



<a name="208586237"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Assign%20to%20slice/near/208586237" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Assign.20to.20slice.html#208586237">(Aug 31 2020 at 16:57)</a>:</h4>
<p>I'd appreciate a bug opened for sxd-document, if it's easy. My guess is that it's some "this requires CDATA here" thing.</p>



<a name="208586270"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Assign%20to%20slice/near/208586270" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gsurrel <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Assign.20to.20slice.html#208586270">(Aug 31 2020 at 16:57)</a>:</h4>
<p>Given how many hours I lost trying to load, mutate individual fields, and save the result, I thought I would be better off finding my delimiters myself and replacing just what needs to be replaced</p>



<a name="208586541"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Assign%20to%20slice/near/208586541" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gsurrel <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Assign.20to.20slice.html#208586541">(Aug 31 2020 at 16:59)</a>:</h4>
<p>The sxd-document problem was more than CDATA, even the few first lines where extremely different: LibreOffice did not recognize it as a Flat ODT file anymore. I assumed that it is too rich in more advanced XML use (lots and lots of namespaces) that it was an unsupported feature of sxd-document</p>



<a name="208586662"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Assign%20to%20slice/near/208586662" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Assign.20to.20slice.html#208586662">(Aug 31 2020 at 17:00)</a>:</h4>
<p>Nah, namespaces were part of SXD from day one. It's certainly possible it's something complicated though.</p>



<a name="208586682"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Assign%20to%20slice/near/208586682" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gsurrel <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Assign.20to.20slice.html#208586682">(Aug 31 2020 at 17:00)</a>:</h4>
<p>I can open a bug-report, once I get time for a minimal demo</p>



<a name="208586705"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Assign%20to%20slice/near/208586705" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Assign.20to.20slice.html#208586705">(Aug 31 2020 at 17:00)</a>:</h4>
<p>yeah, that'd be wonderful — and no hurry</p>



<a name="208586780"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Assign%20to%20slice/near/208586780" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Assign.20to.20slice.html#208586780">(Aug 31 2020 at 17:01)</a>:</h4>
<p>Sounds like "create libreoffice file, open it with SXD, save it with SXD, try to open with libreoffice" :-)</p>



<a name="208587199"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Assign%20to%20slice/near/208587199" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gsurrel <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Assign.20to.20slice.html#208587199">(Aug 31 2020 at 17:04)</a>:</h4>
<p>It is, with a twist: "Save from LibreOffice as Flat ODT" rather than plain ODT. That way, there is no archive to reconstruct (that I could not rebuild even with an unzip-zip :( )</p>



<a name="208587240"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Assign%20to%20slice/near/208587240" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gsurrel <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Assign.20to.20slice.html#208587240">(Aug 31 2020 at 17:04)</a>:</h4>
<p>I wanted to provide a template file and code for a nicer issue</p>



<a name="208587292"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Assign%20to%20slice/near/208587292" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Assign.20to.20slice.html#208587292">(Aug 31 2020 at 17:05)</a>:</h4>
<p>Yeah, absolutely appreciated.</p>



<a name="208587413"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Assign%20to%20slice/near/208587413" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gsurrel <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Assign.20to.20slice.html#208587413">(Aug 31 2020 at 17:06)</a>:</h4>
<p>(By the way, the link to the documentation throws a 404: <a href="http://jakegoulding.com/sxd-document/">http://jakegoulding.com/sxd-document/</a>)</p>



<a name="208587451"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Assign%20to%20slice/near/208587451" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Assign.20to.20slice.html#208587451">(Aug 31 2020 at 17:06)</a>:</h4>
<p>Eww. Where is that link from?</p>



<a name="208587470"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Assign%20to%20slice/near/208587470" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Assign.20to.20slice.html#208587470">(Aug 31 2020 at 17:07)</a>:</h4>
<p>(SXD predates <a href="http://docs.rs">docs.rs</a>)</p>



<a name="208588246"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Assign%20to%20slice/near/208588246" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gsurrel <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Assign.20to.20slice.html#208588246">(Aug 31 2020 at 17:15)</a>:</h4>
<p>It's from the <a href="http://crates.io">crates.io</a> description page:<br>
"An XML library in Rust.<br>
Documentation"</p>



<a name="208588628"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Assign%20to%20slice/near/208588628" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Assign.20to.20slice.html#208588628">(Aug 31 2020 at 17:19)</a>:</h4>
<p>Ah, the crate metadata</p>



<a name="208588637"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Assign%20to%20slice/near/208588637" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Assign.20to.20slice.html#208588637">(Aug 31 2020 at 17:19)</a>:</h4>
<p>thanks</p>



<a name="208588681"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Assign%20to%20slice/near/208588681" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gsurrel <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Assign.20to.20slice.html#208588681">(Aug 31 2020 at 17:19)</a>:</h4>
<p>Issue opened <a href="https://github.com/shepmaster/sxd-document/issues/81">sxd-document#81</a></p>



<a name="208588806"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Assign%20to%20slice/near/208588806" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Assign.20to.20slice.html#208588806">(Aug 31 2020 at 17:20)</a>:</h4>
<p>Back to what I mentioned earlier, you can use all of these as your "place to store the replaced value": <code>Vec&lt;&amp;str&gt;</code> or a <code>Vec&lt;Cow&lt;str&gt;&gt;</code> or a <code>String</code>, all with different tradeoffs</p>



<a name="208589018"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Assign%20to%20slice/near/208589018" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gsurrel <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Assign.20to.20slice.html#208589018">(Aug 31 2020 at 17:22)</a>:</h4>
<p>Is there something wrong with my Vec&lt;(range, &amp;str)&gt;, on which I iterate with:?</p>
<div class="codehilite"><pre><span></span><code><span class="w">        </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="n">range</span><span class="p">,</span><span class="w"> </span><span class="n">value</span><span class="p">)</span><span class="w"> </span><span class="k">in</span><span class="w"> </span><span class="n">replacements</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">            </span><span class="n">template</span><span class="p">.</span><span class="n">replace_range</span><span class="p">(</span><span class="n">range</span><span class="p">,</span><span class="w"> </span><span class="n">value</span><span class="p">);</span><span class="w"></span>
<span class="w">        </span><span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="208589147"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Assign%20to%20slice/near/208589147" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Assign.20to.20slice.html#208589147">(Aug 31 2020 at 17:24)</a>:</h4>
<p>Probably not <em>wrong</em>. There's a possibility it's inefficient.</p>



<a name="208589193"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Assign%20to%20slice/near/208589193" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Assign.20to.20slice.html#208589193">(Aug 31 2020 at 17:24)</a>:</h4>
<p>as presumably each call to <code>replace_range</code> can result in N memory reallocations.</p>



<a name="208589456"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Assign%20to%20slice/near/208589456" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Assign.20to.20slice.html#208589456">(Aug 31 2020 at 17:26)</a>:</h4>
<p>And there's always the point of "is this important enough to worry about".</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>